﻿using System;
using System.Collections.Generic;
using System.Text;

using Pixysoft.Framework.XSchEmA.Aspect;
using Pixysoft.Framework.XSchEmA.Entity;

namespace Pixysoft.Framework.XSchEmA.Sql
{
    class SqlServerSqlBuilder : ASqlBuilder
    {
        private static volatile SqlServerSqlBuilder instance;

        private static object syncRoot = new Object();

        public static SqlServerSqlBuilder Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        instance = new SqlServerSqlBuilder();
                    }
                }

                return instance;

            }
        }

        public override string CreateTable(DatabaseSchema database, TableSchema table)
        {
            if (table == null)
                return null;

            StringBuilder builder = new StringBuilder();
            builder.Append("CREATE TABLE " + table.TableName + "\r\n");
            builder.Append("(");
            foreach (ColumnSchema column in table.Columns)
            {
                builder.Append("\r\n");
                builder.Append(" " + column.ColumnName + "\t");
                builder.Append(GetDataType(column.DataType));
                if (column.DataType.IsAutoIncrement)
                    builder.Append("\tIDENTITY(1,1)");
                builder.Append(CreateDefaultValue(column));
                builder.Append(CreateNullable(column));
                builder.Append(",");
            }
            builder.Remove(builder.Length - 1, 1);
            builder.Append("\r\n");
            builder.Append(")");


            //builder.Append(";");
            return builder.ToString();
        }
        public override string AlterTableModifyColumn(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " ALTER \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(CreateDefaultValue(column));
            //builder.Append(";");
            return builder.ToString();
        }
        public override string AlterTableModifyColumnNullable(ColumnSchema column)
        {
            if (column == null)
                return null;
            StringBuilder builder = new StringBuilder();
            builder.Append("ALTER TABLE " + column.TableName + " ALTER \r\n");
            builder.Append(" " + column.ColumnName + "\t");
            builder.Append(GetDataType(column.DataType));
            builder.Append(CreateStrongNullable(column));
            //builder.Append(";");
            return builder.ToString();
        }
        public override string AlterTableDropIndex(IndexSchema index)
        {
            if (index == null)
                return null;

            //DROP INDEX {0}.{1}

            StringBuilder builder = new StringBuilder();

            builder.Append(string.Format("DROP INDEX {0}.{1}",
                index.TableName, index.ConsName));

            return builder.ToString();

        }
    }
}
